import 'package:flutter/material.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/empty_widget.dart';

class RefreshAndScrollWidget<T> extends StatelessWidget {
  final VoidCallback? onRefresh;
  final ScrollController? scrollController;
  final List<T>? items;
  final Widget Function(T item, num index) itemBuilder;
  final int? itemCount;
  final double top;
  final double bottom;
  final Color color;
  final String? emptyText;

  const RefreshAndScrollWidget({
    super.key,
    this.onRefresh,
    this.scrollController,
    this.items,
    required this.itemBuilder,
    this.itemCount,
    this.top = 0,
    this.bottom = 0,
    this.color = Colors.white,
    this.emptyText = '暂无内容',
  });

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
      backgroundColor: Colors.transparent,
      color: color,
      onRefresh: () {
        if (onRefresh != null) {
          onRefresh!();
        }
        return Future.value();
      },
      child:
          (items == null || items!.isEmpty)
              ? ListView(
                physics: const AlwaysScrollableScrollPhysics(),
                children: [EmptyWidget(emptyText: '$emptyText')],
              )
              : ListView.builder(
                physics: const AlwaysScrollableScrollPhysics(),
                padding: EdgeInsets.only(
                  top: top,
                  bottom: (bottom > 16.px) ? bottom : 16.px,
                ),
                controller: scrollController,
                itemBuilder:
                    (context, index) => itemBuilder(items![index], index),
                itemCount: itemCount,
              ),
    );
  }
}
